import analyse
import sys
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QFileDialog, QTextEdit
)

class DragDropTextEdit(QTextEdit):
    def __init__(self, parent=None, callback=None):
        super().__init__(parent)
        self.setReadOnly(True)
        self.setAcceptDrops(True)
        self.callback = callback  # 用于处理文件的回调函数

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()  # 接受拖拽操作
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()  # 允许在文本框上移动文件
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
            file_path = event.mimeData().urls()[0].toLocalFile()

            if file_path.endswith('.xlsx'):
                self.setText(f"Uploaded: {file_path}")
                if self.callback:
                    self.callback(file_path)
            else:
                self.setText("Invalid file format. Please upload an Excel file.")
        else:
            event.ignore()

class ExcelProcessor(QMainWindow):
    def __init__(self):
        super().__init__()

        self.data_file_path = None
        self.template_file_path = None

        self.initUI()

    def initUI(self):
        self.setWindowTitle("Excel Processor")
        self.setGeometry(100, 100, 600, 400)

        # 设置中心小部件
        self.central_widget = QWidget(self)
        self.setCentralWidget(self.central_widget)

        # 设置布局
        self.layout = QVBoxLayout(self.central_widget)

        # 创建步骤1：上传数据文件
        self.step1_label = QLabel("Step 1: Upload Data File", self)
        self.layout.addWidget(self.step1_label)

        self.step1_text = DragDropTextEdit(self, callback=self.set_data_file)
        self.step1_text.setPlaceholderText("Drag and drop a data file here or click the button below.")
        self.layout.addWidget(self.step1_text)

        self.step1_button = QPushButton("Upload Data File", self)
        self.step1_button.clicked.connect(self.upload_data_file)
        self.layout.addWidget(self.step1_button)

        self.step1_status = QLabel("No file uploaded", self)
        self.layout.addWidget(self.step1_status)

        # 创建步骤2：上传模板文件
        self.step2_label = QLabel("Step 2: Upload Template File", self)
        self.layout.addWidget(self.step2_label)

        self.step2_text = DragDropTextEdit(self, callback=self.set_template_file)
        self.step2_text.setPlaceholderText("Drag and drop a template file here or click the button below.")
        self.layout.addWidget(self.step2_text)

        self.step2_button = QPushButton("Upload Template File", self)
        self.step2_button.clicked.connect(self.upload_template_file)
        self.layout.addWidget(self.step2_button)

        self.step2_status = QLabel("No file uploaded", self)
        self.layout.addWidget(self.step2_status)

        # 创建步骤3：下载处理后的模板文件
        self.step3_label = QLabel("Step 3: Download Processed Template File", self)
        self.layout.addWidget(self.step3_label)

        self.step3_button = QPushButton("Download Processed File", self)
        self.step3_button.clicked.connect(self.download_processed_file)
        self.layout.addWidget(self.step3_button)

        self.step3_status = QLabel("", self)
        self.layout.addWidget(self.step3_status)

    def set_data_file(self, file_path):
        self.data_file_path = file_path
        self.step1_status.setText(f"Uploaded: {file_path}")
        print(f"Data file loaded: {file_path}")

    def set_template_file(self, file_path):
        self.template_file_path = file_path
        self.step2_status.setText(f"Uploaded: {file_path}")
        print(f"Template file loaded: {file_path}")

    def upload_data_file(self):
        # 打开文件对话框以选择数据文件
        file_path, _ = QFileDialog.getOpenFileName(self, "Open Data File", "", "Excel Files (*.xlsx)")
        if file_path:
            self.set_data_file(file_path)
        else:
            self.step1_status.setText("No file uploaded")

    def upload_template_file(self):
        # 打开文件对话框以选择模板文件
        file_path, _ = QFileDialog.getOpenFileName(self, "Open Template File", "", "Excel Files (*.xlsx)")
        if file_path:
            self.set_template_file(file_path)
        else:
            self.step2_status.setText("No file uploaded")

    # 下载
    def download_processed_file(self):


        save_path, _ = QFileDialog.getSaveFileName(self, "Save Processed File", "", "Excel Files (*.xlsx)")

        if save_path:
            analyse.main(self.data_file_path, self.template_file_path,save_path)
        else:
            self.step3_status.setText("File save canceled")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = ExcelProcessor()
    ex.show()
    sys.exit(app.exec_())
